package com.ds.common.excel.convert;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class ListStringConverter implements Converter<List<String>> {
    @Override
    public Class<List> supportJavaTypeKey() {
        return List.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(List list, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        if (CollectionUtils.isEmpty(list)) {
            return new WriteCellData<>("");
        }
        //写excel文件时被EasyExcel调用
        //用json转换工具将List对象转换为json字符串
        String json = JSONObject.toJSONString(list);
        return new WriteCellData<>(json);
    }
}
